﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
#region using more api for personal demand
using System.Text.RegularExpressions;
#endregion using more api for personal demand

namespace Com.ProjectA.Utility
{
    /// <summary>
    /// Using Regular Expressions To Detect Patterns
    /// Author: HoangDNK
    /// Created: 17/04/2013
    /// </summary>
    public class RegexUtil
    {
        private Regex regex;
        /// <summary>
        /// Structure with pattern string without Regular Options
        /// <para>Caution: strPattern must be not null</para>
        /// </summary>
        /// <param name="strPattern">pattern string. Caution: it must be not null</param>
        public RegexUtil(string strPattern)
        {
            regex = new Regex(strPattern);
        }
        /// <summary>
        /// Structure with pattern string with Regular Options
        /// <para>Caution: strPattern must be not null</para>
        /// </summary>
        /// <param name="strPattern">pattern string. Caution: it must be not null</param>
        /// <param name="regexOptions">RegexOptions of .Net 4</param>
        public RegexUtil(string strPattern, RegexOptions regexOptions)
        {
            regex = new Regex(strPattern, regexOptions);
        }
        /// <summary>
        /// Check Exists Pattern In Input String
        /// </summary>
        /// <param name="strInput">Input String</param>
        /// <returns>boolean. True: Matched - False: Unmatched</returns>
        public bool CheckExists(string strInput)
        {
            try
            {
                if (String.IsNullOrWhiteSpace(strInput))
                {
                    return false;
                }
                return regex.IsMatch(strInput.Trim());
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        /// <summary>
        /// Find First Matched String In Input String
        /// <para>Caution: Can be null</para>
        /// </summary>
        /// <param name="strInput">Input String</param>
        /// <returns>Matched/Capture String at the first occurence</returns>
        public string FindMatchedString(string strInput)
        {
            Match match = null;
            try
            {
                if (String.IsNullOrWhiteSpace(strInput))
                {
                    return null;
                }
                match = regex.Match(strInput);
                if (match == null)
                {
                    return null;
                }
                return match.Value;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        /// <summary>
        /// Find Matched String Array Corresponding to pattern in input string
        /// <para>Caution: Can be null</para>
        /// </summary>
        /// <param name="strInput">Input String</param>
        /// <returns>String Array</returns>
        public string[] FindMatchedStringArray(string strInput)
        {
            MatchCollection matchCollection = null;
            string[] arrResult = null;
            int count = 0;
            try
            {
                if (String.IsNullOrWhiteSpace(strInput))
                {
                    return null;
                }
                matchCollection = regex.Matches(strInput);
                if (matchCollection == null)
                {
                    return null;
                }
                arrResult = new string[matchCollection.Count];
                foreach (Match m in matchCollection)
                {
                    arrResult[count++] = m.Value;
                }
                return arrResult;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
    }
}
